package org.zero.controller;

import javax.servlet.http.Cookie;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Zero
 */
@Controller
@RequestMapping("/cookie_value")
public class CookieValueController {
    /*
      CookieValue：
      作用：获取指定名称的cookie
      属性：
        name：同value一样，用于指定cookie名称
        required：默认true。是否必须有指定名称的cookie
     */

    /**
     * 获取cookie值
     *
     * @param cookie
     * @return
     */
    @GetMapping("/m1")
    private String method1(Model model, @CookieValue("JSESSIONID") String cookie) {
        System.out.println("方法1：cookie：" + cookie);
        model.addAttribute("cookie", cookie);
        return "success";
    }

    /**
     * 获取cookie对象
     *
     * @param cookie
     * @return
     */
    @GetMapping("/m2")
    private String method2(Model model, @CookieValue("JSESSIONID") Cookie cookie) {
        System.out.println("方法2：cookie：" + cookie);
        model.addAttribute("cookie", cookie);
        return "success";
    }
}
